package cn.topevery.garden.wapi.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.topevery.garden.dal.logic.FacilitiesFileLogic;
import cn.topevery.garden.dal.logic.FacilitiesLogic;
import cn.topevery.garden.entity.dto.FacilitiesDTO;
import cn.topevery.garden.entity.po.Facilities;
import cn.topevery.garden.entity.query.FacilitiesQuery;
import cn.topevery.garden.entity.vo.FacilitiesPageVO;
import cn.topevery.garden.entity.vo.FacilitiesVO;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "")
@RestController
@RequestMapping("/facilities")
public class FacilitiesController extends BaseController {


    @Autowired
    FacilitiesLogic facilitiesLogic;

    @Autowired
    private FacilitiesFileLogic fileLogic;

    @PostMapping(value = "/getList")
    @ApiOperation(value = "查询设施列表")
    public JsonResult<?> getList(@RequestBody FacilitiesQuery query) {
        return JsonResult.ok(facilitiesLogic.listPage(query));
    }

    @PostMapping(value = "/getListSearch")
    @ApiOperation(value = "根据类型查询设施列表")
    public JsonResult<?> getFacilitiesListByType(@RequestBody FacilitiesQuery query){
        return JsonResult.ok(facilitiesLogic.getFacilitiesListByType(query.getName(),query.getExtentId(),query.getTypeId()));
    }

    @PostMapping(value = "/getFacilitiesPoi")
    @ApiOperation(value = "根据类型查询设施列表")
    public JsonResult<?> getFacilitiesPoi(@RequestBody FacilitiesQuery query){
        return JsonResult.ok(facilitiesLogic.getFacilitiesPoiListByType(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/save")
    public JsonResult<?> saveOrUpdate(@Valid @RequestBody FacilitiesDTO param) {
        facilitiesLogic.saveInfo(getUser().id.toString(), param);
        return JsonResult.ok("保存成功");
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id查询设施")
    public JsonResult<?> getById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        FacilitiesVO facilities = facilitiesLogic.getById(id);
        facilities.setFileList(fileLogic.getByMainId(id));
        return JsonResult.ok(facilities);
    }

    @GetMapping(value = "/delById")
    @ApiOperation(value = "通过id删除")
    public JsonResult<?> delById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        facilitiesLogic.delete(getUser().id.toString(), id);
        return JsonResult.ok("删除成功");
    }


    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        facilitiesLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(FacilitiesQuery query, HttpServletResponse response) {
        List<FacilitiesPageVO> list = facilitiesLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", Facilities.class, "", response);
    }

    @ApiOperation("excel数据导入")
    @PostMapping("/analysisExcel")
    public JsonResult<?> analysisExcel(@RequestBody MultipartFile file) {
        return facilitiesLogic.analysisExcel(file, getUser());
    }
}
